package com.lyn.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyn.common.constant.WareConstant;
import com.lyn.common.utils.PageUtils;
import com.lyn.common.utils.Query;
import com.lyn.gulimall.ware.MergeVo;
import com.lyn.gulimall.ware.dao.PurchaseDao;
import com.lyn.gulimall.ware.entity.PurchaseDetailEntity;
import com.lyn.gulimall.ware.entity.PurchaseEntity;
import com.lyn.gulimall.ware.service.PurchaseDetailService;
import com.lyn.gulimall.ware.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService detailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceivePurchase(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId=mergeVo.getPurchaseId();
        if (purchaseId==null){
            //1,新建一个
           PurchaseEntity purchaseEntity= new PurchaseEntity();
           purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
           purchaseEntity.setCreateTime(new Date());
           purchaseEntity.setUpdateTime(new Date());
           this.save(purchaseEntity);
           purchaseId=purchaseEntity.getId();
        }
            List<Long> items=mergeVo.getItems();
            Long finalPurchaseId=purchaseId;
            List<PurchaseDetailEntity> collect=items.stream().map(i->{
                PurchaseDetailEntity datailEntity=new PurchaseDetailEntity();

                datailEntity.setId(i);
                datailEntity.setPurchaseId(finalPurchaseId);
                datailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
                return datailEntity;
            }).collect(Collectors.toList());
            detailService.updateBatchById(collect);
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setId(purchaseId);
            purchaseEntity.setUpdateTime(new Date());
            this.updateById(purchaseEntity);

    }

}